<?php 

session_start();

require('setlang.php');
require('Models/basics.php');
require('Models/ariane.php');
require("Models/connectBDD.php");
	
$Ariane = getArianeGenealogy();

$connexion = PDO_BDD_Connect();

if (isValidSessionUser($connexion) && areValidUserRights($connexion))
{
	header('Location:'.$_SESSION['userRights']->label.'.php');
}

//Si l'utilisateur et le mot de passe sont rentrés
if (isset($_POST['user']) && isset($_POST['password']))
{
	
	$_POST['user'] = htmlspecialchars($_POST['user']);

	//Si les données sont correctes, on crée la session sinon on affiche une erreur
	if (isValidUser($connexion,$_POST['user'],sha1($_POST['password'])))
	{
		$_SESSION['user'] = $_POST['user'];
		$_SESSION['userid'] = getUserId($connexion,$_POST['user'],sha1($_POST['password']));
		$_SESSION['defaultlanguage'] = getUserLang($connexion,$_SESSION['userid']);
		$_SESSION['userRights'] = getUserRights($connexion,$_SESSION['userid']);
		$_SESSION['passhash'] = sha1($_POST['password']);
		header('Location:index.php');
	}
	else
	{
		$loginError = true;
	}

}

require("Views/login.php");

?>
